package com.souku.finance.application.ui.more;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.souku.finance.application.R;
import com.souku.finance.application.ui.login.LoginActivity;
import com.souku.finance.application.base.BaseFragment;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.widget.BackgroundRelativeLayout;
import com.souku.finance.application.widget.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by Android on 2016/12/5.
 */

public class MoreFragment extends BaseFragment implements View.OnClickListener {

    protected View rootView;
    public static RoundImageView ivHead;
    protected RelativeLayout rlHead;
    protected BackgroundRelativeLayout moreRelative1;
    protected BackgroundRelativeLayout moreRelative2;
    protected BackgroundRelativeLayout moreRelative3;
    protected BackgroundRelativeLayout moreRelative4;
    protected BackgroundRelativeLayout moreRelative5;
    protected LinearLayout llLeft;
    protected LinearLayout llMiddle;
    protected LinearLayout llRight;
    protected TextView tvMobile;
    protected TextView tvVip;
    protected TextView textProgress;
    protected ProgressBar progress;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fra_more, container, false);
        setContent(rootView);
        initView(rootView);
        return rootView;
    }

    //头像
    private void setHeadImage(String url) {
        if (MyTextUtil.isEmpty(url)) {
            ivHead.setImageResource(R.drawable.touxiang);
        } else {
            ImageLoader.getInstance().displayImage(url, ivHead);
        }
    }

    //基本信息
    private void setData() {
        if (ShareUtil.getInstance().getBoolean(Constant.IsLogin)) {
            String userName = ShareUtil.getInstance().getString(Constant.UserName);
//            tvMobile.setText(MyTextUtil.phoneToPass(userName));
            tvMobile.setText(userName);
            tvVip.setText("Lv" + ShareUtil.getInstance().getString(Constant.MemberLevel));
            int maxPoint = Integer.parseInt(ShareUtil.getInstance().getString(Constant.MaxPoint));
            int cumulatedPoint = Integer.parseInt(ShareUtil.getInstance().getString(Constant.CumulatedPoint));
            progress.setMax(maxPoint);
            progress.setProgress(cumulatedPoint);
            textProgress.setText(cumulatedPoint + "/" + maxPoint);
        } else {
            tvMobile.setText("请先登录");
            tvVip.setText("Vip");
            progress.setMax(0);
            progress.setProgress(0);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.ll_right) {
            MobclickAgent.onEvent(getActivity(), "more_activityCenter");
            Intent intent = new Intent(getActivity(), ActivityActivity.class);//活动中心
            startActivity(intent);
        } else if (view.getId() == R.id.more_relative1) {
            MobclickAgent.onEvent(getActivity(), "more_notice");
            Intent intent = new Intent(getActivity(), NoticeActivity.class);//消息公告
            startActivity(intent);
        } else if (view.getId() == R.id.more_relative2) {
            MobclickAgent.onEvent(getActivity(), "more_customerService");
            Intent intent = new Intent(getActivity(), CustomerserviceActivity.class);//客户服务
            startActivity(intent);
        } else if (view.getId() == R.id.more_relative3) {
            MobclickAgent.onEvent(getActivity(), "more_helpCenter");
            Intent intent = new Intent(getActivity(), HelpCenterActivity.class);//帮助中心
            startActivity(intent);
        } else if (view.getId() == R.id.more_relative4) {
            MobclickAgent.onEvent(getActivity(), "more_feedback");
            Intent intent = new Intent(getActivity(), FeedbackActivity.class);//反馈建议
            startActivity(intent);
        } else if (view.getId() == R.id.more_relative5) {
            MobclickAgent.onEvent(getActivity(), "more_about");
            Intent intent = new Intent(getActivity(), AboutUsActivity.class);//关于首库
            startActivity(intent);
        } else if (!ShareUtil.getInstance().getBoolean(Constant.IsLogin)) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.rl_head) {
            MobclickAgent.onEvent(getActivity(), "more_head");
            Intent intent = new Intent(getActivity(), AccountCenterActivity.class);//账户中心
            startActivity(intent);
        } else if (view.getId() == R.id.ll_left) {
            MobclickAgent.onEvent(getActivity(), "more_vip");
            Intent intent = new Intent(getActivity(), VipActivity.class);//会员中心
            startActivity(intent);
        } else if (view.getId() == R.id.ll_middle) {
            MobclickAgent.onEvent(getActivity(), "more_request");
            Intent intent = new Intent(getActivity(), RequestActivity.class);//邀请有礼
            startActivity(intent);
        }
    }

    private void initView(View rootView) {
        ivHead = (RoundImageView) rootView.findViewById(R.id.iv_head);
        rlHead = (RelativeLayout) rootView.findViewById(R.id.rl_head);
        rlHead.setOnClickListener(MoreFragment.this);
        moreRelative1 = (BackgroundRelativeLayout) rootView.findViewById(R.id.more_relative1);
        moreRelative1.setOnClickListener(MoreFragment.this);
        moreRelative2 = (BackgroundRelativeLayout) rootView.findViewById(R.id.more_relative2);
        moreRelative2.setOnClickListener(MoreFragment.this);
        moreRelative3 = (BackgroundRelativeLayout) rootView.findViewById(R.id.more_relative3);
        moreRelative3.setOnClickListener(MoreFragment.this);
        moreRelative4 = (BackgroundRelativeLayout) rootView.findViewById(R.id.more_relative4);
        moreRelative4.setOnClickListener(MoreFragment.this);
        moreRelative5 = (BackgroundRelativeLayout) rootView.findViewById(R.id.more_relative5);
        moreRelative5.setOnClickListener(MoreFragment.this);
        llLeft = (LinearLayout) rootView.findViewById(R.id.ll_left);
        llLeft.setOnClickListener(MoreFragment.this);
        llMiddle = (LinearLayout) rootView.findViewById(R.id.ll_middle);
        llMiddle.setOnClickListener(MoreFragment.this);
        llRight = (LinearLayout) rootView.findViewById(R.id.ll_right);
        llRight.setOnClickListener(MoreFragment.this);
        tvMobile = (TextView) rootView.findViewById(R.id.tv_mobile);
        tvVip = (TextView) rootView.findViewById(R.id.tv_vip);
        textProgress = (TextView) rootView.findViewById(R.id.text_progress);
        progress = (ProgressBar) rootView.findViewById(R.id.progress);
    }

    @Override
    public void internet() {
    }

    @Override
    public void showFragment() {
        setHeadImage(ShareUtil.getInstance().getString(Constant.AvatarUrl));
        setData();
    }
}
